#include "gamewidget.h"
#include "ui_gamewidget.h"
#include "rankinglist.h"
#include <iostream>

using namespace std;

GameWidget::GameWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GameWidget)
{
    ui->setupUi(this);

    for(int i = 0; i <4; i++)   //矩阵初始化为0
        for(int j = 0; j <4; j++)
            gameMatrix[i][j] = 0;

    judgeMove = 0;  //初始化

    display();
    ui->btnUp->setVisible(false);
    ui->btnDown->setVisible(false);
    ui->btnLeft->setVisible(false);
    ui->btnRight->setVisible(false);
    ui->btnReset->setVisible(false);

    connect(&rankingList,&RankingList::returnGameWidge,this,&GameWidget::returnBack);
}

GameWidget::~GameWidget()
{
    delete ui;
}
void GameWidget::returnBack()   //从排行榜返回
{
    rankingList.hide();
    show();
    for(int i = 0; i <4; i++)   //矩阵初始化为0
        for(int j = 0; j <4; j++)
            gameMatrix[i][j] = 0;
    display();
    ui->btnUp->setVisible(false);
    ui->btnDown->setVisible(false);
    ui->btnLeft->setVisible(false);
    ui->btnRight->setVisible(false);
    ui->btnReset->setVisible(false);
    ui->btnStart->setVisible(true);
}

void GameWidget::generateRandom()  //产生随机数
{
    int zeroNumber = 0;//元素中为零的个数
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
            if(gameMatrix[i][j] == 0)
                zeroNumber++;
    }

    if(zeroNumber != 0 && judgeMove == 1)
    {
        judgeMove = 0;
        srand((unsigned)time(nullptr));
        int newNumber = rand()%10 < 9 ? 2 : 4;   //新随机数的值
        int changePlace = rand()%zeroNumber;  //第changePlace个为0元素将会被替代
        for(int i = 0; i < 4; i++)
        {
            for(int j = 0; j < 4; j++)
            {
                if(gameMatrix[i][j] == 0)
                {
                    if(changePlace == 0)
                    {
                        gameMatrix[i][j] = newNumber;
                        changePlace--;

                        break;
                    }
                    else
                        changePlace--;
                }
            }
            if(changePlace < 0)
            {
                break;
            }
        }
    }
}

int GameWidget::sameElment()   //判断是否有相邻的相等项或=0的项
{
    for (int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            if(gameMatrix[i][j]==0)
            {
                return 1;
            }
        }
    }
    for (int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if(gameMatrix[i][j]==gameMatrix[i][j+1] || gameMatrix[i][j] == gameMatrix[i+1][j])
                return 1;
        }
        if(gameMatrix[i][3] == gameMatrix[i+1][3])
            return 1;
    }

    return 0;
}

void GameWidget::changMatrixUp()  //Up修改矩阵元素
{

    for(int i = 0; i < 4; i++)
    {
        for(int k = 0; k < 3; k++)
            for(int j = 3; j > 0; j--)
            {
                if(gameMatrix[j-1][i] == 0)
                {
                    gameMatrix[j-1][i] = gameMatrix[j][i];
                    gameMatrix[j][i] = 0;
                    if(gameMatrix[j-1][i] != 0)
                        judgeMove = 1;
                }
            }

    }

    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if(gameMatrix[j][i] == gameMatrix[j+1][i])
            {
                gameMatrix[j][i] = gameMatrix[j][i]*2;
                if(gameMatrix[j][i] != gameMatrix[j+1][i])
                    judgeMove = 1;
                switch(j)
                {
                    case 0:
                    {
                        gameMatrix[j+1][i] = gameMatrix[j+2][i];
                        gameMatrix[j+2][i] = gameMatrix[j+3][i];
                        gameMatrix[j+3][i] = 0;
                        break;
                    }
                    case 1:
                    {
                        gameMatrix[j+1][i] = gameMatrix[j+2][i];
                        gameMatrix[j+2][i] = 0;
                        break;
                    }
                    case 2:
                    {
                        gameMatrix[j+1][i] = 0;
                        break;
                    }
                }

            }
        }
    }


}

void GameWidget::changMatrixDown()  //Down修改矩阵元素
{
    for(int i = 0; i < 4; i++)
    {
        for(int k = 0; k < 3; k++)
            for(int j = 0; j < 3; j++)
            {
                if(gameMatrix[j+1][i] == 0)
                {
                    gameMatrix[j+1][i] = gameMatrix[j][i];
                    if(gameMatrix[j+1][i] != 0)
                        judgeMove = 1;
                    gameMatrix[j][i] = 0;
                }
            }

    }

    for(int i = 0; i < 4; i++)
    {
        for(int j = 3; j > 0; j--)
        {
            if(gameMatrix[j][i] == gameMatrix[j-1][i])
            {
                gameMatrix[j][i] = gameMatrix[j][i]*2;
                if(gameMatrix[j][i] != gameMatrix[j-1][i])
                    judgeMove = 1;
                switch(j)
                {
                    case 3:
                    {
                        gameMatrix[j-1][i] = gameMatrix[j-2][i];
                        gameMatrix[j-2][i] = gameMatrix[j-3][i];
                        gameMatrix[j-3][i] = 0;
                        break;
                    }
                    case 2:
                    {
                        gameMatrix[j-1][i] = gameMatrix[j-2][i];
                        gameMatrix[j-2][i] = 0;
                        break;
                    }
                    case 1:
                    {
                        gameMatrix[j-1][i] = 0;
                        break;
                    }
                }
            }
        }

    }

}

void GameWidget::changMatrixLeft()  //Left修改矩阵元素
{
    int intermediate;
    for(int i = 0; i < 4; i++)
    {
        for(int j = 3; j > i; j--)
        {
            intermediate = gameMatrix[i][j];
            gameMatrix[i][j] = gameMatrix[j][i];
            gameMatrix[j][i] = intermediate;
        }

    }
    changMatrixUp();
    for(int i = 0; i < 4; i++)
    {
        for(int j = 3; j > i; j--)
        {
            intermediate = gameMatrix[i][j];
            gameMatrix[i][j] = gameMatrix[j][i];
            gameMatrix[j][i] = intermediate;
        }

    }

}

void GameWidget::changMatrixRight()  //Right修改矩阵元素
{
    int intermediate;
    for(int i = 0; i < 4; i++)
    {
        for(int j = 3; j > i; j--)
        {
            intermediate = gameMatrix[i][j];
            gameMatrix[i][j] = gameMatrix[j][i];
            gameMatrix[j][i] = intermediate;
        }

    }
    changMatrixDown();
    for(int i = 0; i < 4; i++)
    {
        for(int j = 3; j > i; j--)
        {
            intermediate = gameMatrix[i][j];
            gameMatrix[i][j] = gameMatrix[j][i];
            gameMatrix[j][i] = intermediate;
        }

    }

}

void GameWidget::display()  //显示
{
    this->ui->label_1->setText(QString::number(gameMatrix[0][0]));
    this->ui->label_2->setText(QString::number(gameMatrix[0][1]));
    this->ui->label_3->setText(QString::number(gameMatrix[0][2]));
    this->ui->label_4->setText(QString::number(gameMatrix[0][3]));
    this->ui->label_5->setText(QString::number(gameMatrix[1][0]));
    this->ui->label_6->setText(QString::number(gameMatrix[1][1]));
    this->ui->label_7->setText(QString::number(gameMatrix[1][2]));
    this->ui->label_8->setText(QString::number(gameMatrix[1][3]));
    this->ui->label_9->setText(QString::number(gameMatrix[2][0]));
    this->ui->label_10->setText(QString::number(gameMatrix[2][1]));
    this->ui->label_11->setText(QString::number(gameMatrix[2][2]));
    this->ui->label_12->setText(QString::number(gameMatrix[2][3]));
    this->ui->label_13->setText(QString::number(gameMatrix[3][0]));
    this->ui->label_14->setText(QString::number(gameMatrix[3][1]));
    this->ui->label_15->setText(QString::number(gameMatrix[3][2]));
    this->ui->label_16->setText(QString::number(gameMatrix[3][3]));

}

void GameWidget::on_btnUp_clicked()
{
    changMatrixUp();
    generateRandom();
    display();

    if(sameElment() == 0)
    {
        rankingList.show();
    }
}

void GameWidget::on_btnDown_clicked()
{
    changMatrixDown();
    generateRandom();
    display();

    if(sameElment() == 0)
    {
        rankingList.show();
    }
}

void GameWidget::on_btnLeft_clicked()
{
    changMatrixLeft();
    generateRandom();
    display();

    if(sameElment() == 0)
    {
        rankingList.show();
    }
}

void GameWidget::on_btnRight_clicked()
{
    changMatrixRight();
    generateRandom();
    display();

    if(sameElment() == 0)
    {
        rankingList.show();
    }
}

void GameWidget::on_btnStart_clicked()
{
    srand((unsigned)time(nullptr));
    int newNumber = rand()%10 < 9 ? 2 : 4;   //新随机数的值
    int changePlace = rand()%16;  //第changePlace个为0元素将会被替代

    gameMatrix[changePlace/4][changePlace%4] = newNumber;
    ui->btnUp->setVisible(true);
    ui->btnDown->setVisible(true);
    ui->btnLeft->setVisible(true);
    ui->btnRight->setVisible(true);
    ui->btnReset->setVisible(true);
    ui->btnStart->setVisible(false);
    display();

}

void GameWidget::on_btnReset_clicked()
{
    for(int i = 0; i <4; i++)   //矩阵初始化为0
        for(int j = 0; j <4; j++)
            gameMatrix[i][j] = 0;
    on_btnStart_clicked();

}
